﻿using Rsd.Dudu;
using Rsd.Dudu.Core;
using Rsd.Dudu.Web;
using Rsd.Dudu.Web.Core;
using Rsd.Dudu.Web.MVC;

using Rsd.Faith.Core;
using Rsd.Faith.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Rsd.Faith.Using.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class UsingController : Rsd.Faith.Core.FaithController
    {
        [ApiService("Index", false)]
        public override object Index()
        {
            //MyType.InitialTable<Models.UsingZiJinShiYongShenQingBiaoModel>( Dudu.DBServices.TableExistOperationType.AlterAndDropColumn);
            //MyType.InitialTable<Models.UsingZiJinShiYongHuaBoBiaoModel>(Dudu.DBServices.TableExistOperationType.AlterAndDropColumn);
            //MyType.InitialTable<Models.UsingZiJinShiYongFenTanBiaoModel>(Dudu.DBServices.TableExistOperationType.AlterAndDropColumn);
            //MyType.InitialTable<Models.UsingZiJinTuiHuoShenQingBiaoModel>(Dudu.DBServices.TableExistOperationType.AlterAndDropColumn);
            //MyType.InitialTable<Models.UsingZiJinShiYongShenQingDtBiaoModel>(Dudu.DBServices.TableExistOperationType.AlterAndDropColumn); 
            
            return base.Index();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="timerService"></param>
        /// <param name="shenQingDanHao"></param>
        /// <param name="huaBoBianHao"></param>
        /// <param name="huaBoRiQi"></param>
        /// <param name="huaBoFangShi"></param>
        /// <param name="huaBoJinE"></param>
        /// <param name="fuKuanZhangHu"></param>
        /// <param name="fuKuanYinHang"></param>
        /// <param name="fuKuanYinHangZhangHao"></param>
        /// <returns></returns>
        [PramaRule("shenQingDanHao", "申请单号(shenQingDanHao)", typeof(string))]
        [PramaRule("huaBoBianHao", "划拨编号(huaBoBianHao)", typeof(string))]
        [PramaRule("huaBoRiQi", "划拨日期(huaBoRiQi)", typeof(string))]
        [PramaRule("huaBoFangShi", "划拨方式(huaBoFangShi)", typeof(string))]
        [PramaRule("huaBoJinE", "划拨金额(huaBoJinE)",0.00,9999999.00)]
        [PramaRule("fuKuanZhangHu", "付款账户类型(fuKuanZhangHu)", typeof(string))]
        [PramaRule("fuKuanYinHang", "付款银行(fuKuanYinHang)", typeof(string), false)]
        [PramaRule("fuKuanYinHangZhangHao", "付款银行账户(fuKuanYinHangZhangHao)", typeof(string),false)]
        [ApiService("使用划拨", true)]
        public object ShenQingHuaBo([Service]IApiTimerService timerService ,string shenQingDanHao, string huaBoBianHao, DateTime huaBoRiQi, BoFuFangShi huaBoFangShi, decimal huaBoJinE, FuKuanZhangHu fuKuanZhangHu,string fuKuanYinHang,string fuKuanYinHangZhangHao)
        {
            var shenqing = this.GetObject<UsingZiJinShiYongShenQingBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);

            if (shenqing == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.U0002, shenQingDanHao);
            }

            var huaBo = this.GetObject<UsingZiJinShiYongHuaBoBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);
            if (huaBo != null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.U0006, shenQingDanHao);
            }

            if (fuKuanZhangHu != FuKuanZhangHu.BJ)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.U0007, shenQingDanHao);
            }
            if (huaBoFangShi == BoFuFangShi.WZ)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.U0008, huaBoFangShi.ToString());
            }

            huaBo = new UsingZiJinShiYongHuaBoBiaoModel();

            huaBo.XiaoQuBianHao = shenqing.XiaoQuBianHao;
            huaBo.ShenQingDanHao = shenQingDanHao;
            huaBo.HuaBoBianHao = shenQingDanHao;
            huaBo.HuaBoRiQi = huaBoRiQi;
            huaBo.SuoShuQi = huaBoRiQi.Year.ToString();
            huaBo.HuaBoFangShi = huaBoFangShi;
            huaBo.HuaBoJinE = huaBoJinE;
            huaBo.FuKuanZhangHu = fuKuanZhangHu;
            huaBo.ZhuangTai = ZiJinHuaBoZhuangTai.DJ;
            //
            huaBo.FuKuanYinHang = fuKuanYinHang;
            huaBo.FuKuanYinHangZhangHao = fuKuanYinHangZhangHao;
            huaBo.ShouKuanYinHang = shenqing.ShouKuanYinHang;
            huaBo.ShouKuanYinHangZhangHao = shenqing.ShouKuanYinHangZhangHao;
            huaBo.SetState(Dudu.BizModel.ObjectStateType.New);
            this.SaveObject(huaBo);

            shenqing.ZhuangTai = ZiJinShenQinZhuangTai.HB;
            shenqing.HuaBoBianHao = huaBo.HuaBoBianHao;
            shenqing.BoFuFangShi = huaBoFangShi;
            shenqing.SetState(Dudu.BizModel.ObjectStateType.Update);
            this.SaveObject(shenqing);

            huaBo = this.GetObject<UsingZiJinShiYongHuaBoBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);
            var _host = this.GetConfigAppSetting("timerHost");
            switch (huaBo.FuKuanZhangHu)
            {
                case FuKuanZhangHu.BJ:
                    //本金账户
                    timerService.NewTimer("HBRZ"+ shenQingDanHao, "Fiscal/HuaBoRuZhang?shenQingDanHao=" + shenQingDanHao, "划拨入账(本金账户)");
                    timerService.StartTimer(_host, "HBRZ" + shenQingDanHao);
                    break;
                case FuKuanZhangHu.TC:  
                case FuKuanZhangHu.XQTC: 
                case FuKuanZhangHu.LDTC: 
                case FuKuanZhangHu.DYTC:
                    //统筹账户
                    timerService.NewTimer("HBRZ" + shenQingDanHao, "Bank/ShiYongBoHua?shenQingDanHao=" + shenQingDanHao, "划拨入账(统筹账户)");
                    timerService.StartTimer(_host, "HBRZ" + shenQingDanHao);
                    break;
                default:
                    break;
                
            }

            return huaBo;
        }
        
        /// <summary>
        /// 同步分摊表 建筑面积 ，在房屋 面积信息 修改后，同步后，重新计算分摊
        /// </summary>
        /// <returns></returns>
        [ApiService("同步分摊数据建筑面积", false)]
        public object TongBuJianZhuMianJi()
        {
            var sql = "UPDATE biz__zi_jin_shi_yong_fen_tan_biao a ,info__fang_wu_xin_xi_biao b set a.jian_zhu_mian_ji=b.jian_zhu_mian_ji where a.fang_wu_bian_hao=b.bian_hao;";
            return "受影响的行:" + this.GetService<IDBSession>().DbService.Execute(sql).ToString();
          
        }
        /// <summary>
        /// 添加分摊房屋
        /// </summary>
        /// <param name="shenQingDanHao"></param>
        /// <param name="fangWuDaiMaList"></param>
        /// <returns></returns>
        [PramaRule("shenQingDanHao", "申请单号(shenQingDanHao)", typeof(string))]
        [PramaRule("fangWuDaiMaList", "房屋代码(fangWuDaiMaList)", typeof(string))]
        [ApiService("添加分摊房屋", true)]
        public object FangWuFengTan(string shenQingDanHao,string fangWuDaiMaList)
        {
            //
            var huaBo = this.GetObject<UsingZiJinShiYongHuaBoBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);
            if (huaBo == null)
            {
                return new ErrorResult<ErrorCodesType>( ErrorCodesType.U0003,shenQingDanHao);
            }
            if (huaBo.ZhuangTai == ZiJinHuaBoZhuangTai.RZ)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.U0004, "已入账");
            }
             
            var daiMaList = fangWuDaiMaList.Split(',');
            if (daiMaList.Length == 0)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.U0009); 
            }

            //删除已有分摊记录
            MyType.GetDBOperator<UsingZiJinShiYongFenTanBiaoModel>().Delete().Where(obj => obj.ShenQingDanHao == shenQingDanHao).Execute<int>();

            //获取分摊房屋信息 自动剔除不存在的房屋  
            var fangWuList = this.GetObjects<InfoFangWuXinXiBiaoModel>(obj => daiMaList.Contains(obj.BianHao));

            if (fangWuList.Length == 0)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.U0009, shenQingDanHao);
            }

            var list = new List<UsingZiJinShiYongFenTanBiaoModel>();
            foreach (var fangWu in fangWuList)
            {
                var fenTan = new UsingZiJinShiYongFenTanBiaoModel();
                fenTan.ShenQingDanHao = shenQingDanHao;
                fenTan.SuoShuQi = huaBo.SuoShuQi;
                fenTan.HuaBoBianHao = huaBo.HuaBoBianHao;
                fenTan.HuaBoRiQi = huaBo.HuaBoRiQi;
                //fenTan.LouDongHao = fangWu.LouDongHao;
                fenTan.XiaoQuBianHao = fangWu.XiaoQuBianHao;
                fenTan.FangWuBianHao = fangWu.BianHao;
                fenTan.JianZhuMianJi = fangWu.JianZhuMianJi;
                fenTan.FenTanJinE = 0;
                fenTan.FenTanHouYuE = 0;
                fenTan.ZhuangTai = FenTanZhuangTai.DJ;
                fenTan.FenTanRiQi = DateTime.Now;
                fenTan.SetState(Dudu.BizModel.ObjectStateType.New);
                list.Add(fenTan);
            }

            this.SaveObjects(list.ToArray());

            return list.Count;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="shenQingDanHao"></param>
        /// <param name="louDongHao"></param>
        /// <returns></returns>
        [PramaRule("shenQingDanHao", "申请单号(shenQingDanHao)", typeof(string))]
        [ApiService("分摊整个楼栋", true)]
        public object FengTanLouDong(string shenQingDanHao, string louDongHao)
        {
            //分摊 楼栋 

            //分摊 户

            //分摊后 核算 楼栋帐 单元 支出 
            return null;
        }
     
        /// <summary>
        /// 
        /// </summary>
        /// <param name="shenQingDanHao"></param>
        /// <param name="louDongHao"></param>
        /// <returns></returns>
        [PramaRule("shenQingDanHao", "申请单号(shenQingDanHao)", typeof(string))]
        [ApiService("分摊整个小区", true)]
        public object FengTanXiaoQu(string shenQingDanHao)
        {
            //分摊 所有 楼栋 

            //分摊 户

            //分摊后 核算 楼栋帐 单元 支出 
            return null;
        }

        /// <summary>
        /// 确认分摊 ，计算分摊金额。该请求调用后，  请求入明细帐（FiscalController.FenTanRuZhangMX）  完整入账
        /// </summary>
        /// <param name="shenQingDanHao"></param>
        /// <returns></returns>
        [PramaRule("shenQingDanHao", "申请单号(shenQingDanHao)", typeof(string))]
        [ApiService("确认分摊，核算分担金额", true)]
        public object FengTanQueRen( string shenQingDanHao)
        {
            var huaBo = this.GetObject<UsingZiJinShiYongHuaBoBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);
            if (huaBo == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.U0003,  shenQingDanHao);
            }
            if (huaBo.ZhuangTai ==  ZiJinHuaBoZhuangTai.RZ)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.U0004,  "已入账");
            }
            //分摊 金额 
            var mianJi = 0.00M;
            var jinE = huaBo.HuaBoJinE;
            var jinE_YuE = jinE;
            var list = this.GetObjects<UsingZiJinShiYongFenTanBiaoModel>(obj=>obj.ShenQingDanHao == shenQingDanHao);

            if (list.Length == 0)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.U0005, shenQingDanHao);
            }
            foreach (var item in list)
            {
                mianJi += item.JianZhuMianJi;
            }

            //单位金额 =  总金额 / 总建筑面积 
            var unit = Math.Round(jinE / mianJi,2);
          
            foreach (var item in list)
            {
                //各户分摊金额 = 单位金额 * 建筑面积
                item.FenTanJinE = Math.Round(unit * item.JianZhuMianJi, 2);
                if (item.FenTanJinE > jinE_YuE)
                {
                    item.FenTanJinE = jinE_YuE;
                }
                jinE_YuE -= item.FenTanJinE;

                //分摊后余额 = 分户余额 - 分摊金额
                //item.FenTanHouYuE = 

                item.SetState( Dudu.BizModel.ObjectStateType.Update);
            }
            if (jinE_YuE > 0)
            {
                list[list.Length - 1].FenTanJinE += jinE_YuE;
            }
            this.SaveObjects<UsingZiJinShiYongFenTanBiaoModel>(list.ToArray());
            //修改 划拨数据状态 为已分摊
            huaBo.ZhuangTai =  ZiJinHuaBoZhuangTai.FT;
            huaBo.SetState( Dudu.BizModel.ObjectStateType.Update);

            return this.SaveObject(huaBo);
       
        }

        /// <summary>
        /// 分摊入账(添加入账定时任务)
        /// </summary>
        /// <param name="shenQingDanHao"></param>
        /// <returns></returns>
        [PramaRule("shenQingDanHao", "申请单号(shenQingDanHao)", typeof(string))]
        [ApiService("分摊入账(添加入账定时任务)", true)]
        public object FengTanRuZhang([Service]IApiTimerService timerService ,string shenQingDanHao)
        {
            var huaBo = this.GetObject<UsingZiJinShiYongHuaBoBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);
            if (huaBo == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.U0003, shenQingDanHao);
            }
            if (huaBo.ZhuangTai == ZiJinHuaBoZhuangTai.RZ)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.U0004, "已入账");
            }

            var _host = this.GetConfigAppSetting("timerHost");
            timerService.NewTimer("FTRZ" + shenQingDanHao, "Fiscal/FenTanRuZhangMX?shenQingDanHao=" + shenQingDanHao, "分摊入账(明细)");
            timerService.NewTimer("FTRZ" + shenQingDanHao, "Fiscal/FenTanRuZhangFH?shenQingDanHao=" + shenQingDanHao, "分摊入账(分户)");
            timerService.StartTimer(_host, "FTRZ" + shenQingDanHao);

            return new { };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="shenQingDanHao"></param>
        /// <returns></returns>
        [PramaRule("shenQingDanHao", "申请单号(shenQingDanHao)", typeof(string))]
        [ApiService("获取使用申请信息", true)]
        public object GetShenQinInfo(string shenQingDanHao)
        {
            return this.GetObject<UsingZiJinShiYongShenQingBiaoModel>(obj=>obj.ShenQingDanHao == shenQingDanHao);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="shenQingDanHao"></param>
        /// <returns></returns>
        [PramaRule("shenQingDanHao", "申请单号(shenQingDanHao)", typeof(string))]
        [ApiService("获取分摊信息", true)]
        public object GetFenTanList(string shenQingDanHao)
        {
            return this.GetObjects<UsingZiJinShiYongFenTanBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);
        }
    }
}
